package com.kingsoft.minibase;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

import com.kingsoft.minibase.KeyValue;
import com.kingsoft.minibase.DiskStore.DefaultCompactor;

public class MStore implements MiniBase{
    private MemStore memStore;
    private ExecutorService pool;
    private DiskStore diskStore;
    private AtomicLong sequenceId;
    private Compactor compactor;
    private Config config;

    // 初始化
    public MiniBase open() throws IOException {
        assert this.config != null;

        // initialize threadPool
        this.pool = Executors.newFixedThreadPool(config.getMaxThreadPoolSize());

        // initialize diskStore
        this.diskStore = new DiskStore(config.getDataDir(), config.getMaxDiskFiles());
        diskStore.open();

        // initialize memStore
        this.memStore = new MemStore(config, new DiskStore.DefaultFlusher(diskStore), pool);

        // initialize sequenceId
        this.sequenceId = new AtomicLong(0);

        //initialize compactor
        DefaultCompactor defaultCompactor = new DefaultCompactor();
        this.compactor = defaultCompactor;
        compactor.start();

        return this;
    }

    private MStore(Config config) {
        this.config = config;
    }

    public void create(Config config) {
        new MStore(config);
    }
    @Override
    public void put(byte[] key, byte[] value) throws IOException {

    }

    @Override
    public KeyValue get(byte[] key) throws IOException {
        return null;
    }

    @Override
    public void delete(byte[] key) throws IOException {

    }

    @Override
    public Iter<KeyValue> scan(byte[] startKey, byte[] stopKey) throws IOException {
        return null;
    }

    interface SeekIter<KeyValue> extends Iter<KeyValue> {
        void seekTo(KeyValue kv) throws IOException;
    }
}
